package com.group15.phms;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.support.v4.app.NavUtils;

public class NoteEditor extends Activity{
	SharedPreferences logins;
	DatabaseHelper db;
	String currentUser;
	String title="";
	Note n;
	boolean newNote=true;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_note_editor);
		// Show the Up button in the action bar.
		getActionBar().setDisplayHomeAsUpEnabled(true);
		
		logins = getSharedPreferences("logins",MODE_PRIVATE);
		currentUser = logins.getString("currentUser","INVALID");
		db= new DatabaseHelper(getApplicationContext());
		if(getIntent().getStringExtra("title")!=null)
		{
			title = getIntent().getStringExtra("title");
			n = db.getNote(currentUser, title);
			((EditText)findViewById(R.id.title)).setText(n.getTitle());
			((EditText)findViewById(R.id.body)).setText(n.getBody());
			((EditText)findViewById(R.id.body)).requestFocus();
			newNote=false;
		}
		db.closeDB();
		
		findViewById(R.id.confirm).setOnClickListener(
				new View.OnClickListener() {
					@Override
					public void onClick(View view) {
						saveNote();
					}
				});
		findViewById(R.id.cancel).setOnClickListener(
				new View.OnClickListener() {
					@Override
					public void onClick(View view) {
						discardNote();
					}
				});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.note_editor, menu);
		return true;
	}
	Intent intent;
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			NavUtils.navigateUpFromSameTask(this);
			return true;
		case R.id.action_logout:
			intent = new Intent(this, Login.class);
	    	startActivity(intent);
	    	finish();
			return true;
		case R.id.action_save:
			saveNote();
			return true;
		case R.id.action_search:
			intent = new Intent(this, Search.class);
	    	startActivity(intent);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	void discardNote()
	{
		finish();
	}
	
	void saveNote()
	{
		db= new DatabaseHelper(getApplicationContext());
		boolean flag=true;
		EditText titleField = (EditText)findViewById(R.id.title);
		EditText bodyField = (EditText)findViewById(R.id.body);
		String newtitle=titleField.getText().toString();
		String body=bodyField.getText().toString();
		
		titleField.setError(null);
		bodyField.setError(null);
		
		if(newtitle.equals(""))
		{
			flag=false;
			titleField.setError("Note must have a title");
		}
		List<String> titles =db.getNoteTitles(currentUser);
		for (String t : titles)
			if (newtitle.equals(t) && !newtitle.equals(title) )
			{
				flag=false;
				titleField.setError("There is already a note with this title");
			}
		
		if(flag==true)
		{
			if(newNote)
			{	
				db.addNote(currentUser,new Note(newtitle, body));
			}
			else //modified note
			{
				Note oldNote=n;
				db.updateNote(currentUser,oldNote,new Note(newtitle, body));
			}
			finish();
		}
		db.closeDB();
	}
}